﻿using System;
using System.Collections.Generic;
using LogSystem;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    [CustomEditor(typeof(FogEditor))]
    public class FogEditorEditor : UnityEditor.Editor
    {
        private static string fogJsonPath = "Assets/ResourcesAssets/City/Config/fogJsonConfig.json";

        private FogEditor _fogEditor;
        
        public static GUIContent fogHeaderText = EditorGUIUtility.TrTextContent("迷雾区域列表");
        private SerializedProperty m_fogListProp;
        private ReorderableList m_fogRenderList;
        
        private void OnEnable()
        {
            _fogEditor = target as FogEditor;
            
            m_fogListProp = serializedObject.FindProperty("subFogConfigs");
            m_fogRenderList = new ReorderableList(serializedObject, m_fogListProp, true, true,true, true);
            DrawFogListLayout(m_fogRenderList, m_fogListProp);
        }

        private Vector2 scrollPos;
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            base.OnInspectorGUI();

            GUILayout.Space(10);


            if (m_fogRenderList!=null)
            {
                m_fogRenderList.DoLayoutList();
            }

            EditorGUILayout.EndScrollView();
            GUILayout.Space(10);

            if (GUILayout.Button("预览迷雾区域",GUILayout.Height(30)))
            {
                _fogEditor.Show();
            }

            if (GUILayout.Button("取消预览",GUILayout.Height(30)))
            {
                _fogEditor.UnShow();
            }

            if (GUILayout.Button("显示迷雾ID",GUILayout.Height(30)))
            {
                _fogEditor.ShowFogId();
            }

            if (GUILayout.Button("隐藏迷雾ID",GUILayout.Height(30)))
            {
                _fogEditor.HideFogId();
            }
            
            serializedObject.ApplyModifiedProperties();
        }

        private List<string> _colorNameList = new List<string>()
        {
            "红色", "绿色", "蓝色", "黄色", "青色"
        };
        private List<Color> _colors = new List<Color>()
        {
            Color.red,
            Color.green,
            Color.blue,
            Color.yellow,
            Color.cyan,
        };

        private void DrawFogListLayout(ReorderableList list, SerializedProperty prop)
        {
            list.elementHeight = EditorGUIUtility.singleLineHeight * 1.25f * 2 + 1;

            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                EditorGUIUtility.labelWidth = 65;
                var element = prop.GetArrayElementAtIndex(index);
                rect.x += 2;
                rect.y += 2;
                SerializedProperty idProperty = element.FindPropertyRelative("fogId");
                Rect idRect = new Rect(rect.x, rect.y, 150, EditorGUIUtility.singleLineHeight);
                idProperty.intValue = EditorGUI.IntField(idRect, "迷雾区域ID", idProperty.intValue);

                rect.y += 2;
                SerializedProperty colorProperty = element.FindPropertyRelative("color");
                EditorGUIUtility.labelWidth = 30;
                float colorRectX = rect.x;
                bool customColor = true;
                for (int i = 0; i < 5; i++)
                {
                    bool s = _colors[i] == colorProperty.colorValue;
                    if (s) {
                        customColor = false;
                    }
                    Rect colorRect = new Rect(colorRectX, rect.y + idRect.height, 70, EditorGUIUtility.singleLineHeight);
                    s = EditorGUI.Toggle(colorRect, _colorNameList[i], s);
                    if (s)
                    {
                        if (i == 1)
                        {
                            int xxxx = 1;
                        }
                        colorProperty.colorValue = _colors[i];
                    }
                    colorRectX += colorRect.width + 5;
                }

                if (customColor)
                {
                    
                }

            };
            
            list.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, fogHeaderText);
            };
            
            list.onCanRemoveCallback = li => { return li.count > 0; };
        }
        
     
    }
}